<?

/**
* Global data backup helper functions
*
* @author Adesanto Asman
* @version 1.0
* @license Free
* @package CGDataBackup
* 
*/

/**
* support functions go here
*
* @package CDataBackup
*
*/
class CDataBackup{
  
  /**
  * Build the data id
  *
  * @param void
  * @return date
  *
  */
  function buildDataId(){
    
    return date("d-m-Y-his");
  }
  
  /**
  * fetch the latest entry by date
  *
  */
  function processLatestEntry(){
    
      
  }
}

/**
* All vars of classOracleDb are define here
* @package CGDataBackup
*
*/
class CGDataBackup extends CDataBackup{
  
  /**
  * it back up the sequence
  *
  * @param  string  $COracle
  * @param  integer $nSeq
  * @param  array   $seqEntryArray
  * @param  string  $srcDbHdr
  * @param  string  $srcDbDtl
  * @return boolean true=success, false=fail
  *
  */
  function backupSequence(&$COracle,
                          &$nSeq,
                          &$seqEntryArray,
                          &$srcDbHdr,
                          &$srcDbDtl){
    
    /**
    * build the dataId
    * it's date with local time
    *
    */
    $dataId     = CDataBackup::buildDataId();
    
    $todayDate  = date("d-m-Y");
    
    CGFetchers::fetchLastNoByDate($COracle,
                                  $todayDate,
                                  $srcDbHdr,
                                  $lastNo);
    
    for($iRow = 0; $iRow < $nSeq; $iRow++){       
    
      $periode = " TO_DATE('".date("d-m-Y",
                                   strtotime($seqEntryArray["PERIODE"][$iRow]))."' ,
                           'dd-mm-YYYY') ";
    
      $values = " '".$dataId."', ".                           /* Data Id */
                "  ".$periode." ,   ".                        /* periode */
                " '".$seqEntryArray["SEQ_NAME"][$iRow]."', ". /* Sequence name    */
                "  ".$seqEntryArray["SEQ_NUMBER"][$iRow]." "; /* Sequence number  */
      
      CGHandlers::insBoSeqDtl($COracle,
                              $values,
                              $srcDbDtl);
    }
    
    $hdrDate = " TO_DATE('".$todayDate."',
                         'dd-mm-YYYY') ";
    
    $values = " '".$dataId."',  ".
              "  ".$hdrDate." , ".
              "  ".$lastNo."    ";
        
    CGHandlers::insBoSeqHdr($COracle,
                            $values,
                            $srcDbHdr);
                            
    if($COracle->getStatus() == true){
      
      usrMsg("Backup process successed");
  	  return true;
  	}else{
  	  
  	  usrMsg("Backup process interrupted");
  	  return false;
  	}
  }
  
  /**
  * Reload the master Sequence
  * with the backup
  * 
  * @param  string  $COracle
  * @param  integer $nSeq
  * @param  array   $seqEntryArray
  * @param  string  $srcDb
  * @return boolean true=success, false=fail
  *
  */
  function reloadLastSequence(&$COracle,
                              &$nSeq,
                              &$seqEntryArray,
                              &$srcDb){
        
    /**
    * update the current working Sequence
    *
    */
    for($iRow = 0; $iRow < $nSeq; $iRow++){
      
      CGHandlers::updateMstSequence($COracle,
                                    $iRow,
                                    $seqEntryArray,
                                    $srcDb);
    }
    
    if($COracle->getStatus() == true){
      
      usrMsg("Reload process successed");
  	  return true;
  	}else{
  	  
  	  usrMsg("Reload process interrupted");
  	  return false;
  	}
  }
  
  function reloadLastSeqManually(){
    
    
  }
}
?>